package com.dc.rabbitmq.sender;

import com.dc.rabbitmq.config.DelayRabbitConfig;
import com.dc.rabbitmq.config.RabbitMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 消息发送  生产者1
 */
@Slf4j
@Component
public class Sender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 普通队列 发送消息
     *
     * @param uuid
     * @param message 消息
     */
    public void sendFirst(String uuid, Object message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setCorrelationId(uuid);
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE, RabbitMqConfig.ROUTINGKEY1, new Message(String.valueOf(message).getBytes(), messageProperties));
    }

    public void sendSecond(String uuid, Object message) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setCorrelationId(uuid);
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE, RabbitMqConfig.ROUTINGKEY2, new Message(String.valueOf(message).getBytes(), messageProperties));
    }

    /**
     * 延迟队列 发送消息
     *
     * @param msg  参数
     * @param time 时间毫秒 6000=6秒
     */
    public void sendDelay(Object msg, Long time) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration(time + "");
        messageProperties.setCorrelationId(UUID.randomUUID().toString());
        Message message = new Message(String.valueOf(msg).getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(DelayRabbitConfig.ORDER_DELAY_EXCHANGE, DelayRabbitConfig.ORDER_DELAY_ROUTING_KEY, message);
    }
}
